#!/usr/bin/env python
# encoding: utf-8

"""
登录程序（带模板）：
GET /：首页，返回Home；
GET /signin：登录页，显示登录表单；
POST /signin：处理登录表单，显示登录结果。
Flask通过Python的装饰器在内部自动地把URL和函数给关联起来
"""

from flask import Flask, request, render_template

app = Flask(__name__)


@app.route("/", methods=['GET', 'POST'])
def home():
    return render_template('home.html')


# http://127.0.0.1:5000/signin
@app.route("/signin", methods=['GET'])
def signin_form():
    return render_template('form.html')


@app.route('/signin', methods=['POST'])
def signin():
    """
    需要从request对象读取表单内容
    :return:
    """
    username = request.form['username']
    password = request.form['password']
    if username == 'admin' and password == 'admin':
        return render_template('signin-ok.html', username=username)
    return render_template('form.html', message='用户名或密码不正确！')


if __name__ == '__main__':
    app.run()
